﻿using BW.Demo.Domain.Entities;
using BW.Demo.ErrorCode;
using BW.Demo.WebApi.Applications.Commands;
using BW.Demo.WebApi.Applications.Commands.Deputy;
using BW.Demo.WebApi.Dtos;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BW.Demo.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ManageController : ControllerBase
    {
        IMediator mediator;
        IWebHostEnvironment env;
        public ManageController(IMediator mediator, IWebHostEnvironment env)
        {
            this.mediator = mediator;
            this.env = env; 
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
         public async Task<APIResult<int>> AddTraceability([FromBody] TraceabilityCreateCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> UpdTraceability([FromBody] TraceabilityUpdateCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> DelTraceability([FromBody] TraceabilityDeleteCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 溯源表显示
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<APIPaging<TraceabilityDTO>>> Show([FromQuery] TraceabilityQueryCommand command)
        {
            return mediator.Send(command);
        }
        /// <summary>
        /// 商品表显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<ProductName>>> GoodsShow( )
        {
            GoodsQueryCommand command = new GoodsQueryCommand();
            return mediator.Send(command);
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Upload(IFormFile file)
        {
            var n=Guid.NewGuid().ToString();
            var path=env.WebRootPath + "/pic/" + n+file.FileName;
                using(var fs=System.IO.File.Create(path))
            {
                file.CopyTo(fs);
                fs.Close();
            }
                return Ok("http://localhost:5127"+"/pic/"+n+file.FileName);
        }
        /// <summary>
        /// 养殖信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<Aquaculture>>> GetAquaculture([FromQuery] AquacultureQueryCommand command)
        {
            return mediator.Send(command);
        }
       
        /// <summary>
        /// 养殖企业信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<BreedingEnterprise>>> GetBreeding([FromQuery] BreedingQueryCommand command)
        {
            return mediator.Send(command);
        }
        /// <summary>
        /// 屠宰信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<SlaughteringInformation>>> GetSlaughteringInformation([FromQuery] SlaugInformQueryCommand command)
        {
            return mediator.Send(command);
        }
        /// <summary>
        /// 屠宰企业信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<SlaughteringEnterprise>>> GetSlaughteringEnterprise([FromQuery] SlaughEnterQueryCommand command)
        {
            return mediator.Send(command);
        }
        /// <summary>
        /// 深加工信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<ProcessingInformation>>> GetProcessingInformation([FromQuery] ProcessformQueryCommand command)
        {
            return mediator.Send(command);
        }
        /// <summary>
        /// 深加工企业信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<ProcessingEnterprise>>> GetProcessingEnterprise([FromQuery] ProcessEnterQueryCommand command)
        {
            return mediator.Send(command);
        }
        /// <summary>
        /// 保健信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<Health>>> GetHealth([FromQuery] HealthQueryCommand command)
        {
            return mediator.Send(command);
        }
        /// <summary>
        /// 图片
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<PictureInformation>>> GetPictureInformation([FromQuery] PicQueryCommand command)
        {
            return mediator.Send(command);
        }
    }
}
